package servlet;

import bean.Category;
import bean.Product;
import bean.PropertyValue;
import net.sf.json.JSONObject;
import util.JSONUtil;
import util.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class PropertyValueServlet extends BaseBackServlet {
    @Override
    public String add(HttpServletRequest request, HttpServletResponse response, Page page) {
        return null;
    }

    @Override
    public String delete(HttpServletRequest request, HttpServletResponse respone, Page page) {
        return null;
    }

    @Override
    public String edit(HttpServletRequest request, HttpServletResponse response, Page page) {
        int pid = Integer.parseInt(request.getParameter("pid"));
        Product product = productDAO.get(pid);
        Category category = product.getCategory();

        propertyValueDAO.init(product);
        List<PropertyValue> values = propertyValueDAO.list(pid);
//        if((values ).size()==0)

        request.setAttribute("category",category);
        request.setAttribute("product",product);
        request.setAttribute("values",values);
        return "jsp/admin/editPropertyValue.jsp";
    }

    @Override
    public String update(HttpServletRequest request, HttpServletResponse response, Page page) {
        JSONObject object = JSONUtil.getJSON(request);
        int pid = Integer.parseInt(object.get("pid").toString());
        int ptid = Integer.parseInt(object.get("ptid").toString());
//        int ptid = (Integer)object.get("ptid");
//        int id = (Integer)object.get("id");
        String value = object.get("value").toString();
        PropertyValue propertyValue = propertyValueDAO.get(ptid,pid);
        propertyValue.setValue(value);
        propertyValueDAO.update(propertyValue);
        return "%success";
    }

    @Override
    public String list(HttpServletRequest request, HttpServletResponse response, Page page) {
        return null;
    }
}
